/**
 * 
 */
package be.bejug.scrumadmin.domain;

import java.io.Serializable;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import be.bejug.scrumadmin.domain.enums.Priority;
import be.bejug.scrumadmin.domain.enums.Status;

/**
 * @author Glenn Dejaeger
 * 
 */
@SuppressWarnings("serial")
@Entity
@Table(name = "STORIES", schema = "SCRUMADMIN")
public class Story extends DomainObject implements Serializable,
		Comparable<Story> {

	@Column(name = "NAME", nullable = false)
	private String name;

	@Column(name = "DESCRIPTION", nullable = false)
	private String description;

	@OneToMany(mappedBy = "story")
	private Set<Task> tasks = new HashSet<Task>();

	@Column(name = "PRIORITY")
	private Priority priority;

	@OneToOne
	@JoinColumn(name = "CREATEDBY", nullable = false)
	private User createdBy;

	@Column(name = "ESTIMATION")
	private Double estimation;

	@ManyToOne
	@JoinColumn(name = "SPRINT_ID", nullable = false)
	private Sprint sprint;

	@Column(name = "STATUS", nullable = false)
	private Status status;

	/**
	 * 
	 */
	public Story() {
		super();
		estimation = 0d;
	}

	/**
	 * @param name
	 * @param description
	 * @param priority
	 * @param createdBy
	 */
	public Story(final int id, final String name, final String description,
			final Priority priority, final User createdBy) {
		this();
		this.id = id;
		this.name = name;
		this.description = description;
		this.priority = priority;
		this.createdBy = createdBy;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(final String name) {
		this.name = name;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description
	 *            the description to set
	 */
	public void setDescription(final String description) {
		this.description = description;
	}

	/**
	 * @return the tasks
	 */
	public Set<Task> getTasks() {
		return tasks;
	}

	/**
	 * @param tasks
	 *            the tasks to add
	 */
	public void addTasks(final Task... tasks) {
		this.tasks.addAll(Arrays.asList(tasks));
		setEstimation();
	}

	/**
	 * @return the priority
	 */
	public Priority getPriority() {
		return priority;
	}

	/**
	 * @param priority
	 *            the priority to set
	 */
	public void setPriority(final Priority priority) {
		this.priority = priority;
	}

	/**
	 * @return the createdBy
	 */
	public User getCreatedBy() {
		return createdBy;
	}

	/**
	 * @param createdBy
	 *            the createdBy to set
	 */
	public void setCreatedBy(final User createdBy) {
		this.createdBy = createdBy;
	}

	/**
	 * @return the estimation
	 */
	private void setEstimation() {
		for (Task task : getTasks()) {
			estimation += task.getEstimation();
		}
		estimation = Math.ceil(estimation);
	}

	public Double getEstimation() {
		return estimation;
	}

	/**
	 * @return the sprint
	 */
	public Sprint getSprint() {
		return sprint;
	}

	/**
	 * @param sprint
	 *            the sprint to set
	 */
	public void setSprint(final Sprint sprint) {
		this.sprint = sprint;
	}

	/**
	 * @return the status
	 */
	public Status getStatus() {
		return status;
	}

	/**
	 * @param status
	 *            the status to set
	 */
	public void setStatus(final Status status) {
		this.status = status;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (prime * result) + ((name == null) ? 0 : name.hashCode());
		// result = (prime * result) + ((sprint == null) ? 0 :
		// sprint.hashCode());
		return result;
	}

	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Story other = (Story) obj;
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		if (sprint == null) {
			if (other.sprint != null) {
				return false;
			}
		} else if (!sprint.equals(other.sprint)) {
			return false;
		}
		return true;
	}

	@Override
	public int compareTo(final Story story) {
		return this.getName().compareTo(story.getName());
	}

}
